import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:smart_shop/constant/constant.dart';
import 'package:smart_shop/route/route.dart';
import 'package:smart_shop/page/order.dart';
import 'package:smart_shop/page/shop.dart';
import 'package:smart_shop/page/my.dart';

void main() {
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarIconBrightness: Brightness.light,
    statusBarColor: Colors.transparent,
  ));
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '北海智慧商圈',
      locale: const Locale('zh'),
      localizationsDelegates: const [
        RefreshLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: const [
        Locale('zh'),
        Locale('en'),
      ],
      debugShowCheckedModeBanner: false,
      initialRoute: '/',
      onGenerateRoute: onGenerateRoute,
      home: const MyHomePage(title: '北海智慧商圈'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int currentIndex = 0;

  List<Widget> pageList = [
    const Order(),
    const Shop(),
    const My(),
  ];

  late PageController controller;

  @override
  void initState() {
    super.initState();
    controller = PageController(initialPage: currentIndex);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: createPageView(context),
      bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
                label: '订单',
                icon: Image.asset('${Constant.ICON_URL}home.png',
                    width: 32, height: 32, fit: BoxFit.contain),
                activeIcon: Image.asset('${Constant.ICON_URL}home_active.png',
                    width: 32, height: 32, fit: BoxFit.contain)),
            BottomNavigationBarItem(
                label: '商铺',
                icon: Image.asset('${Constant.ICON_URL}shop.png',
                    width: 32, height: 32, fit: BoxFit.contain),
                activeIcon: Image.asset('${Constant.ICON_URL}shop_active.png',
                    width: 32, height: 32, fit: BoxFit.contain)),
            const BottomNavigationBarItem(
                label: '我的', icon: Icon(Icons.perm_identity))
          ],
          unselectedLabelStyle: const TextStyle(
              fontFamily: 'PingFangSC-Regular',
              fontWeight: FontWeight.w400,
              color: Color.fromRGBO(178, 178, 178, 1)),
          selectedLabelStyle: const TextStyle(
              fontFamily: 'PingFangSC-Regular',
              fontWeight: FontWeight.w400,
              color: Color.fromRGBO(107, 142, 246, 1)),
          currentIndex: currentIndex,
          onTap: (index) => {
                setState(() {
                  currentIndex = index;
                  controller.jumpToPage(index);
                })
              },
          iconSize: 32,
          backgroundColor: Colors.white,
          useLegacyColorScheme: false,
          type: BottomNavigationBarType.fixed),
    );
  }

  PageView createPageView(context) {
    return PageView(
      physics: const NeverScrollableScrollPhysics(),
      controller: controller,
      children: pageList,
    );
  }
}
